package com.airena.service.impl;

import com.airena.entity.dto.RoleInfoDTO;
import com.airena.entity.dto.RoleInfoPageQueryDTO;
import com.airena.entity.po.RoleCategory;
import com.airena.entity.po.RoleInfo;
import com.airena.exception.BaseException;
import com.airena.mapper.RoleInfoMapper;
import com.airena.service.IRoleCategoryService;
import com.airena.service.IRoleInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author mender
 * @since 2025-09-23
 */
@Service
public class RoleInfoServiceImpl extends ServiceImpl<RoleInfoMapper, RoleInfo> implements IRoleInfoService {

    @Autowired
    private RoleInfoMapper roleInfoMapper;

    @Autowired
    private IRoleCategoryService roleCategoryService;

    /**
     * 根据角色id获取角色系统提示词
     * @param roleId
     * @return
     */
    @Override
    public String getRoleInfoString(Long roleId) {
        // 检查roleId是否为null
        if (roleId == null) {
            throw new BaseException("角色ID不能为空");
        }

        RoleInfo roleInfo = roleInfoMapper.selectById(roleId);
        LambdaQueryWrapper<RoleCategory> eq = new QueryWrapper<RoleCategory>().lambda()
                .eq(roleInfo.getCategoryCode()!=null,RoleCategory::getCategoryCode, roleInfo.getCategoryCode());
        RoleCategory category = roleCategoryService.getOne(eq);
        String s = toIntroduction(roleInfo,category);
        if (s == null) {
            throw new BaseException("角色信息不存在");
        }
        return s;
    }

    /**
     * 根据角色id获取角色信息
     * @param roleId
     * @return
     */
    @Override
    public RoleInfo getRoleInfo(Long roleId) {
        if (roleId == null) {
            throw new BaseException("角色ID不能为空");
        }
        return roleInfoMapper.selectById(roleId);
    }


    /**
     * 分页查询角色信息
     *
     * @param pageQueryDTO
     * @return
     */
    @Override
    public Page<RoleInfo> queryRolePage(RoleInfoPageQueryDTO pageQueryDTO) {
        Page<RoleInfo> page = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        RoleInfo roleInfo = new RoleInfo();
        BeanUtils.copyProperties(pageQueryDTO, roleInfo);
        LambdaQueryWrapper<RoleInfo> eq = new QueryWrapper<RoleInfo>().lambda()
                .eq(pageQueryDTO.getStatus() != null, RoleInfo::getStatus, pageQueryDTO.getStatus())
                .eq(pageQueryDTO.getCategory() != null, RoleInfo::getCategoryCode, pageQueryDTO.getCategory());
        return roleInfoMapper.selectPage(page, eq);
    }

    /**
     * 删除角色信息
     * @param roleId
     * @return
     */
    @Override
    public int deleteRoleInfo(Long roleId) {
        return roleInfoMapper.deleteById(roleId);
    }

    /**
     * 修改角色信息
     * @param roleInfoDTO
     * @return
     */
    @Override
    public int updateRoleInfo(RoleInfoDTO roleInfoDTO) {
        RoleInfo roleInfo = new RoleInfo();
        BeanUtils.copyProperties(roleInfoDTO, roleInfo);
        roleInfo.setCategoryCode(roleInfoDTO.getCategory());
        roleInfo.setUpdateTime(LocalDateTime.now());
        return roleInfoMapper.updateById(roleInfo);
    }

    /**
     * 新增角色信息
     * @param roleInfoDTO
     */
    @Override
    public void addRoleInfo(RoleInfoDTO roleInfoDTO) {
        RoleInfo roleInfo = new RoleInfo();
        BeanUtils.copyProperties(roleInfoDTO, roleInfo);
        roleInfo.setCategoryCode(roleInfoDTO.getCategory());
//        默认状态为审核中
        roleInfo.setStatus(2);
        roleInfo.setCreateTime(LocalDateTime.now());
        roleInfo.setUpdateTime(LocalDateTime.now());
        boolean save = this.save(roleInfo);
        if (!save)
            throw new BaseException("新增角色信息失败");
    }

    /**
     * 拼接角色介绍字符串
     */
    public String toIntroduction(RoleInfo roleInfo,RoleCategory category) {
        StringBuilder sb = new StringBuilder();

        if (notEmpty(roleInfo.getRoleName())) {
            sb.append("你叫").append(roleInfo.getRoleName());
        }
        if (category != null && notEmpty(category.getCategoryName())) {
            sb.append("，是").append(category.getCategoryName()).append("中的角色");
        }
        if (notEmpty(roleInfo.getSource())) {
            sb.append("，来自").append(roleInfo.getSource());
        }
        if (notEmpty(roleInfo.getPersonality())) {
            sb.append("，具有").append(roleInfo.getPersonality()).append("的性格");
        }
        if (notEmpty(roleInfo.getLanguageStyle())) {
            sb.append("，语言风格是").append(roleInfo.getLanguageStyle());
        }
        if (notEmpty(roleInfo.getBackgroundKnowledge())) {
            sb.append("，掌握的背景知识包括：").append(roleInfo.getBackgroundKnowledge());
        }

        // 如果一句话都没拼出来，返回一个默认描述
        return sb.length() > 0 ? sb.toString() + "。" : null;
    }

    /**
     * 判断字符串是否非空
     */
    private boolean notEmpty(String str) {
        return str != null && !str.trim().isEmpty();
    }

}
